import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:largefriends/entity/entities/lf_bool_entity.dart';
import 'package:largefriends/events/user_relation_event.dart';
import 'package:largefriends/service/manager/lf_event_helper.dart';
import 'package:largefriends/net/error_handle.dart';
import 'package:largefriends/net/lf_api_url.dart';
import 'package:largefriends/net/lf_dio_util.dart';
import 'package:largefriends/res/strings/lf_strings.dart';
import 'package:largefriends/utils/lf_dialog_utils.dart';
import 'package:largefriends/utils/lf_keyboard.dart';
import 'package:largefriends/widgets/dialog_action_tip.dart';

import '../../config/lf_route.dart';

toReportPage({
  required String userId,
  String timelineId = "",
}) {
  Get.toNamed(LFRoute.pathReport, arguments: {
    'userId': userId,
    'timelineId': timelineId,
  });
}

class LfReportLogic extends GetxController {
  TextEditingController controller = TextEditingController();
  FocusNode focusNode = FocusNode();
  // RxInt reason = 0.obs;
  RxList<int> reason = RxList.empty(growable: true);
  RxString content = "".obs;
  RxBool alsoBlock = false.obs;
  late String userId, timelineId;

  bool get enableReport {
    if (reason.isEmpty) {
      return false;
    } else if (reason.length == 1 && reason.first == 16) {
      return content.value.trim().isNotEmpty;
    } else {
      return true;
    }
  }

  @override
  void onInit() {
    userId = Get.arguments['userId'];
    timelineId = Get.arguments['timelineId'];
    super.onInit();
  }

  @override
  void onClose() {
    closeSoftKeyboard();
    controller.dispose();
    focusNode.dispose();
    super.onClose();
  }

  report() {
    closeSoftKeyboard();
    showLoading();
    Map<String, String> params = {};
    params['userId'] = userId;
    if (timelineId.isNotEmpty) {
      params['timelineId'] = timelineId;
      params['reportType'] = "6";
    }
    params['content'] = content.value.trim();
    params['commentId'] = reason.join(',');
    if (alsoBlock.isTrue) {
      params['block'] = "1";
    } else {
      params['block'] = "0";
    }

    LfDio().post<LfBoolEntity>(url: apiReport, params: params).then((value) {
      hideLoading();
      showActionTips(
          tips: strReportSuccessfulTip.tr,
          action: () {
            Get.back();
            if (alsoBlock.isTrue) {
              LfEventHelper().getEvent().fire(
                  UserRelationEvent(uid: userId, type: UserRelationType.block));
            }
          },
          dismissible: false,
          showCancel: false);
    }, onError: (error) {
      hideLoading();
      if (error is NetError) {
        showSnackBar(error.msg);
      }
    });
  }
}
